home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CMacPrimer.cpt / EventTutor ƒ / EventTutor.c < prev   
C/C++ Source or Header  |  1990-03-02  |  8KB  |  366 lines

  1. #define BASE_RES_ID            400
  2. #define NIL_POINTER            0L
  3. #define MOVE_TO_FRONT        -1L
  4. #define REMOVE_ALL_EVENTS    0
  5.  
  6. #define LEAVE_WHERE_IT_IS    FALSE
  7. #define NORMAL_UPDATES        TRUE
  8.  
  9. #define SLEEP                0L
  10. #define NIL_MOUSE_REGION    0L
  11. #define WNE_TRAP_NUM        0x60
  12. #define UNIMPL_TRAP_NUM        0x9F
  13. #define SUSPEND_RESUME_BIT    0x0001
  14. #define ACTIVATING            1
  15. #define RESUMING            1
  16.  
  17. #define TEXT_FONT_SIZE        12
  18.  
  19. #define DRAG_THRESHOLD        30
  20. #define MIN_WINDOW_HEIGHT    50
  21. #define MIN_WINDOW_WIDTH    50
  22. #define    SCROLL_BAR_PIXELS    16
  23.  
  24. #define    ROWHEIGHT            15
  25. #define LEFTMARGIN            10
  26. #define STARTROW            0
  27. #define HORIZONTAL_OFFSET    0
  28.  
  29.  
  30.  
  31. PicHandle    gPictureHandle;
  32. WindowPtr    gPictWindow, gEventWindow;
  33. Boolean        gDone, gWNEImplemented;
  34. EventRecord    gTheEvent;
  35. int            gCurRow, gMaxRow;
  36. Rect        gDragRect, gSizeRect;
  37.  
  38. /****************** main *********************/
  39.  
  40. main()
  41. {
  42.     ToolBoxInit();
  43.     WindowInit();
  44.     LoadPicture();
  45.     SetUpDragRect();
  46.     SetUpSizeRect();
  47.     
  48.     MainLoop();
  49. }
  50.  
  51. /******************** ToolBoxInit ***************/
  52.  
  53. ToolBoxInit()
  54. {
  55.     InitGraf(&thePort);
  56.     InitFonts();
  57.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(NIL_POINTER);
  62.     InitCursor();
  63. }
  64.  
  65. /******************** WindowInit *******************/
  66.  
  67. WindowInit()
  68. {
  69.     gPictWindow = GetNewWindow(BASE_RES_ID,NIL_POINTER,
  70.         MOVE_TO_FRONT);
  71.     gEventWindow = GetNewWindow(BASE_RES_ID+1,NIL_POINTER,
  72.         MOVE_TO_FRONT);
  73.     SetPort(gEventWindow);
  74.     SetupEventWindow();
  75.     
  76.     ShowWindow(gEventWindow);
  77.     ShowWindow(gPictWindow);
  78.     
  79.     SelectWindow(gEventWindow);
  80. }
  81.  
  82. /*********************SetUpEventWindow ****************/
  83.  
  84. SetupEventWindow()
  85.  
  86. {
  87.     Rect    eventRect;
  88.     
  89.     eventRect = gEventWindow->portRect;
  90.     gMaxRow = eventRect.bottom - eventRect.top - ROWHEIGHT;
  91.     gCurRow = STARTROW;
  92.     
  93.     TextFont (monaco);
  94.     TextSize (TEXT_FONT_SIZE);
  95.     
  96. }
  97.  
  98. /**************** LoadPicture ********************/
  99.  
  100. LoadPicture()
  101. {
  102.     gPictureHandle = GetPicture(BASE_RES_ID);
  103. }
  104.  
  105. /***************** SetUpDragRect ******************/
  106.  
  107. SetUpDragRect()
  108. {
  109.     gDragRect = screenBits.bounds;
  110.     gDragRect.left += DRAG_THRESHOLD;
  111.     gDragRect.right -= DRAG_THRESHOLD;
  112.     gDragRect.bottom -= DRAG_THRESHOLD;
  113. }
  114.  
  115. /******************** SetUpSizeRect ******************/
  116.  
  117. SetUpSizeRect()
  118. {
  119.     gSizeRect.top = MIN_WINDOW_HEIGHT;
  120.     gSizeRect.left = MIN_WINDOW_WIDTH;
  121.     
  122.     gSizeRect.bottom = screenBits.bounds.bottom - screenBits.bounds.top;
  123.     gSizeRect.right = screenBits.bounds.right - screenBits.bounds.left;
  124. }
  125.  
  126. /****************** MainLoop ***************************/
  127.  
  128. MainLoop()
  129. {
  130.     gDone = FALSE;
  131.     gWNEImplemented = (NGetTrapAddress (WNE_TRAP_NUM, ToolTrap) !=
  132.             NGetTrapAddress (UNIMPL_TRAP_NUM, ToolTrap ));
  133.     while (gDone == FALSE)
  134.     {
  135.         HandleEvent();
  136.     }
  137. }
  138.  
  139. /******************** HandleEvent ***********************/
  140.  
  141. HandleEvent()
  142. {
  143.     if (gWNEImplemented)
  144.         WaitNextEvent (everyEvent, &gTheEvent, SLEEP, NIL_MOUSE_REGION);
  145.     else
  146.     {
  147.         SystemTask();
  148.         GetNextEvent (everyEvent, &gTheEvent);
  149.     }
  150.     
  151.     switch (gTheEvent.what)
  152.     {
  153.         case nullEvent:
  154.             /* DrawEventString ("\pnullEvent");*/
  155.             break;
  156.         case mouseDown:
  157.             DrawEventString ("\pmouseDown");
  158.             HandleMouseDown();
  159.             break;
  160.         case mouseUp:
  161.             DrawEventString ("\pmouseUp");
  162.             break;
  163.         case keyDown:
  164.             DrawEventString("\pkeyDown");
  165.             break;
  166.         case keyUp:
  167.             DrawEventString ("\pkeyUp");
  168.         case autoKey:
  169.             DrawEventString ("\pautoKey");
  170.             break;
  171.         case updateEvt:
  172.             if ((WindowPtr)gTheEvent.message == gPictWindow)
  173.             {
  174.                 DrawEventString ("\pupdateEvt: gPictWindow");
  175.                 BeginUpdate (gTheEvent.message);
  176.                 DrawMyPicture (gTheEvent.message, gPictureHandle);
  177.                 EndUpdate (gTheEvent.message);
  178.             } else
  179.             {
  180.                 DrawEventString ("\pupdateEvt: gEventWindow");
  181.                 BeginUpdate (gTheEvent.message);
  182.                 EndUpdate (gTheEvent.message);
  183.             }
  184.             break;
  185.         case diskEvt:
  186.             DrawEventString ("\pdiskEvt");
  187.             break;
  188.         case activateEvt:
  189.             if ((WindowPtr)gTheEvent.message == gPictWindow)
  190.             {
  191.                 DrawGrowIcon(gTheEvent.message);
  192.                 if ((gTheEvent.modifiers & activeFlag)== ACTIVATING)
  193.                 {
  194.                     DrawEventString (
  195.                     "\pactivateEvt: activating gPictWindow");
  196.                 } else
  197.                     DrawEventString(
  198.                     "\pactivateEvt: deactivating gPictWindow");
  199.             } else
  200.             {
  201.                 if ((gTheEvent.modifiers & activeFlag) == ACTIVATING)
  202.                     DrawEventString (
  203.                     "\pactivateEvt: activating gEvent Window");
  204.                 else
  205.                     DrawEventString(
  206.                     "\pactivateEvt: deactivating gEventWindow");
  207.             }
  208.             break;
  209.         case networkEvt:
  210.             DrawEventString ("\pnetworkEvt");
  211.             break;
  212.         case driverEvt:
  213.             DrawEventString ("\pdriverEvt");
  214.             break;
  215.         case app1Evt:
  216.             DrawEventString ("\papp1Evt");
  217.             break;
  218.         case app2Evt:
  219.             DrawEventString ("\papp2Evt");
  220.             break;
  221.         case app3Evt:
  222.             DrawEventString ("\papp3Evt");
  223.             break;
  224.         case app4Evt:
  225.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  226.                 DrawEventString ("\pResume event");
  227.             else
  228.                 DrawEventString ("\pSuspend event");
  229.             break;
  230.     }
  231. }
  232.  
  233. /*************** DrawEventString ***********************/
  234.  
  235. DrawEventString(s)
  236. Str255 s;
  237. {
  238.     if (gCurRow > gMaxRow)
  239.     {
  240.         ScrollWindow();
  241.     }
  242.     else
  243.     {
  244.         gCurRow += ROWHEIGHT;
  245.     }
  246.     MoveTo (LEFTMARGIN, gCurRow);
  247.     DrawString (s);
  248. }
  249.  
  250. /********************* ScrollWindow *************************/
  251.  
  252. ScrollWindow()
  253. {
  254.     RgnHandle    tempRgn;
  255.     
  256.     tempRgn = NewRgn();
  257.     ScrollRect (&gEventWindow->portRect, HORIZONTAL_OFFSET,
  258.              -ROWHEIGHT, tempRgn);
  259.     DisposeRgn (tempRgn);
  260. }
  261.  
  262. /********************** HandleMouseDown ********************/
  263.  
  264. HandleMouseDown()
  265. {
  266.     WindowPtr    whichWindow;
  267.     short int    thePart;
  268.     long        windSize;
  269.     GrafPtr        oldPort;
  270.     
  271.     thePart = FindWindow (gTheEvent.where, &whichWindow);
  272.     switch (thePart)
  273.     {
  274.         case inSysWindow:
  275.             SystemClick(&gTheEvent, whichWindow);
  276.             break;
  277.         case inDrag:
  278.             DragWindow (whichWindow, gTheEvent.where, &gDragRect);
  279.             break;
  280.         case inContent:
  281.             SelectWindow (whichWindow);
  282.             break;
  283.         case inGrow:
  284.             windSize = GrowWindow(whichWindow, gTheEvent.where, &gSizeRect);
  285.             if (windSize != 0)
  286.             {
  287.                 GetPort (&oldPort);
  288.                 SetPort (whichWindow);
  289.                 EraseRect (&whichWindow->portRect);
  290.                 SizeWindow (whichWindow, LoWord (windSize),
  291.                     HiWord (windSize), NORMAL_UPDATES);
  292.                 InvalRect (&whichWindow->portRect);
  293.                 SetPort (oldPort);
  294.             }
  295.             break;
  296.         case inGoAway:
  297.             gDone = TRUE;
  298.             break;
  299.         case inZoomIn:
  300.         case inZoomOut:
  301.             if (TrackBox (whichWindow, gTheEvent.where, thePart))
  302.             {
  303.                 GetPort (&oldPort);
  304.                 SetPort (whichWindow);
  305.                 EraseRect (&whichWindow->portRect);
  306.                 ZoomWindow (whichWindow, thePart, LEAVE_WHERE_IT_IS);
  307.                 InvalRect (&whichWindow->portRect);
  308.                 SetPort(oldPort);
  309.             }
  310.             break;
  311.     }
  312. }
  313.     
  314. /********************* DrawMyPicture ********************/
  315.  
  316. DrawMyPicture(drawingWindow, thePicture)
  317. WindowPtr    drawingWindow;
  318. PicHandle    thePicture;
  319. {
  320.     Rect drawingClipRect, myRect;
  321.     GrafPtr    oldPort;
  322.     RgnHandle    tempRgn;
  323.     
  324.     GetPort (&oldPort);
  325.     SetPort (drawingWindow);
  326.     tempRgn = NewRgn();
  327.     GetClip (tempRgn);
  328.     EraseRect (&drawingWindow->portRect);
  329.     DrawGrowIcon (drawingWindow);
  330.     
  331.     drawingClipRect = drawingWindow->portRect;
  332.     drawingClipRect.right -= SCROLL_BAR_PIXELS;
  333.     drawingClipRect.bottom -= SCROLL_BAR_PIXELS;
  334.     
  335.     myRect = drawingWindow->portRect;
  336.     CenterPict(thePicture, &myRect);
  337.     ClipRect (&drawingClipRect);
  338.     DrawPicture(thePicture, &myRect);
  339.     
  340.     SetClip (tempRgn);
  341.     DisposeRgn (tempRgn);
  342.     SetPort (oldPort);
  343. }
  344.  
  345. /*********************** CenterPict *******************/
  346.  
  347. CenterPict(thePicture, myRectPtr)
  348. PicHandle    thePicture;
  349. Rect        *myRectPtr;
  350. {
  351.     Rect    windRect, pictureRect;
  352.     
  353.     windRect= *myRectPtr;
  354.     pictureRect = (**(thePicture)).picFrame;
  355.     myRectPtr->top = (windRect.bottom - windRect.top -
  356.         (pictureRect.bottom - pictureRect.top))/2 +
  357.         windRect.top;
  358.     myRectPtr->bottom = myRectPtr->top +
  359.         (pictureRect.bottom - pictureRect.top);
  360.     myRectPtr->left = (windRect.right - windRect.left -
  361.         (pictureRect.right - pictureRect.left))/2 +
  362.         windRect.left;    
  363.     myRectPtr->right = myRectPtr->left + (pictureRect.right - 
  364.         pictureRect.left);
  365. }
  366.